package com.ybwh.openapi3;

import com.ybwh.outterapi.anno.OpenApi;
import com.ybwh.outterapi.constant.RequestHeaderProperties;
import io.swagger.v3.oas.models.Operation;
import io.swagger.v3.oas.models.parameters.HeaderParameter;
import org.springdoc.core.customizers.OperationCustomizer;
import org.springframework.web.method.HandlerMethod;

public class OutterApiOperationCustomizer implements OperationCustomizer {
    @Override
    public Operation customize(Operation operation, HandlerMethod handlerMethod) {
        OpenApi openApi = handlerMethod.getMethodAnnotation(OpenApi.class);
        if (null == openApi) {
            openApi = handlerMethod.getBeanType().getAnnotation(OpenApi.class);
        }

        if (null == openApi) {
            return operation;
        }

        HeaderParameter appkeyParam = new HeaderParameter();
        appkeyParam.required(true);
        appkeyParam.setName(RequestHeaderProperties.APP_KEY);
        appkeyParam.setDescription("接入方注册的app_key");
        operation.addParametersItem(appkeyParam);

        HeaderParameter hashParam = new HeaderParameter();
        hashParam.required(true);
        hashParam.setName(RequestHeaderProperties.CHECK_HASH);
        hashParam.setDescription("请求体的hash码，SHA-256算法计算");
        operation.addParametersItem(hashParam);


//        MediaType md = operation.getRequestBody().getContent().get("application/json");
//        md.getSchema().description("此接口为外部系统调用接口,body内容传根据app_secret加密后的数据");

        return operation;
    }
}
